package com.cuckoo.cuckoo_app2.apiAsync.Imp;

import android.util.Log;

import com.cuckoo.cuckoo_app2.apiAsync.BaseCallbac;
import com.cuckoo.cuckoo_app2.apiAsync.WalletApi;
import com.cuckoo.cuckoo_app2.config.ApiConfig;
import com.cuckoo.cuckoo_app2.dao.WithdrawalInformation;
import com.cuckoo.cuckoo_app2.utils.NetworkRequestHelperUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class WalletAPiImp implements WalletApi {

    private Gson gson;
    private ApiConfig apiConfig;

    public WalletAPiImp() {
        this.gson = new Gson();
        this.apiConfig = new ApiConfig();
    }

    @Override
    public void performGetWalletAsync(String userId, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuWalletaMnagement/getByUserId";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url + "?userId=" + userId;
                Log.i("HTTP", "GET:" + http);
                try {
                    Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performGetWalletListAsync(HashMap<String, String> params, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuWalletaTransaction/list";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                http += "?" + NetworkRequestHelperUtils.mapToUrlParams(params);
                Log.i("HTTP", "GET:" + http);

                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performSubmitWithdrawAsync(WithdrawalInformation withdrawalInformation, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuWithdrawalRecord/insertWithdrawalRecord";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(withdrawalInformation), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }

            }
        }).start();
    }
}
